//
//  YTWebViewVC.m
//  CllForIOS
//
//  Created by qiyun on 2018/3/14.
//  Copyright © 2018年 aRui. All rights reserved.
//

#import "YTWebViewVC.h"
#import <WebKit/WebKit.h>
#import "YTHomeViewController.h"
@interface YTWebViewVC ()<WKNavigationDelegate>
@property (strong, nonatomic) WKWebView* webView;
@property (nonatomic, strong) UIProgressView *progressView;

@property (strong, nonatomic) UIActivityIndicatorView *activityIndicatorView;

@end

@implementation YTWebViewVC
- (UIStatusBarStyle)preferredStatusBarStyle {

    if([self.urlStr rangeOfString:@"findList_message"].location !=NSNotFound){
      return UIStatusBarStyleDefault;
    }
    if ([self.urlStr isEqual:@"https://m.qyun88.com/lobby/activity.shtml"]) {
       return UIStatusBarStyleDefault;
    }
    
    return UIStatusBarStyleLightContent;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    [self setnav];
    [self.view addSubview:self.webView];
    [self.view addSubview:self.progressView];
    if (@available(iOS 11.0, *)) {
        self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    // 加载网页
    [self.webView  loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_urlStr]]];
    if (_urlStr) {
         [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew  context:nil];
    }
    
    self.activityIndicatorView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 32, 32)];
    self.activityIndicatorView.center = self.view.center;
    [self.activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [self.view addSubview:self.activityIndicatorView];
    
//    //在注册界面添加返回按钮
//    if ([self.urlStr isEqualToString:@"http://m.qyun88.com/member/reg.shtml"]) {
    UIButton*leftbtn =[[UIButton alloc]initWithFrame:CGRectMake(5, k_statusbarHeight+15, 20, 20)];
    if ([self.urlStr isEqual:@"https://m.qyun88.com/lobby/activity.shtml"]) {
        self.statusView.backgroundColor = [UIColor  whiteColor];
        leftbtn =[[UIButton alloc]initWithFrame:CGRectMake(15, k_statusbarHeight+15, 20, 20)];
    }

        [leftbtn setImage:IMAGE(@"back") forState:UIControlStateNormal];
        [leftbtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:leftbtn];
//    }
    
    
}
#pragma mark KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change  context:(void *)context
{
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        self.progressView.progress = self.webView.estimatedProgress;
        // 加载完成
        if (self.webView.estimatedProgress  >= 1.0f ) {
            [UIView animateWithDuration:0.5f animations:^{
                self.progressView.alpha = 0.0f;
                self.progressView.progress = 0.0f;
            }];
            
        }else{
            self.progressView.alpha = 1.0f;
        }
        
    }
    
}
-(void)setnav{
    self.titleLabel.text=self.webtitle;
    self.titleLabel.textColor=[UIColor whiteColor];
//    self.topView.backgroundColor=RGBA(223, 33, 53);
    self.topView.hidden=YES;
    self.statusView.backgroundColor = RGBA(223, 33, 53);
    if ([self.urlStr isEqual:@"https://m.qyun88.com/kj/index.php?platform=app"]) {
        self.statusView.backgroundColor = [UIColor  whiteColor];
    }
    
    if([self.urlStr rangeOfString:@"findList_message"].location !=NSNotFound){
        self.statusView.backgroundColor = [UIColor  whiteColor];
    }
    if ([self.urlStr isEqual:@"https://m.qyun88.com/lobby/activity.shtml"]) {
        self.statusView.backgroundColor = [UIColor  whiteColor];
    }
    
    self.lineView.hidden=YES;
    [self addLeftTarger:@selector(back)];
    
    [self.leftButton setTitle:@"首页" forState:UIControlStateNormal];
    [self.leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.leftButton.titleLabel.font = KFONT_medium(16);
    
}
-(void)back{
    [self.navigationController popViewControllerAnimated:1];
}
#pragma mark LazyLoad
- (WKWebView *)webView {
    if (!_webView) {
        _webView = [[WKWebView alloc] init];
        _webView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        _webView.backgroundColor = [UIColor whiteColor];
//        _webView.scrollView.scrollEnabled=NO;
        _webView.scrollView.showsVerticalScrollIndicator = NO;
        _webView.scrollView.showsHorizontalScrollIndicator = NO;
        _webView.navigationDelegate=self;
        _webView.frame = CGRectMake(0, k_statusbarHeight, SCREEN_W, SCREEN_H -  k_statusbarHeight);//49
        if (iPhoneX) {
            _webView.frame = CGRectMake(0, k_statusbarHeight, SCREEN_W, SCREEN_H -  k_statusbarHeight-34);
        }
    }
    return _webView;
}
- (UIProgressView *)progressView {
    if (!_progressView) {
        _progressView = [[UIProgressView alloc] init];
        _progressView.frame = CGRectMake(0, k_statusbarHeight+44, [UIScreen mainScreen].bounds.size.width, 2);
    }
    return _progressView;
}
- (void)dealloc {
    if (_urlStr) {
        [self.webView removeObserver:self forKeyPath:@"estimatedProgress" context:nil];
    }
}
#pragma mark - **************** delegate
/**
 *  开始加载网页
 */
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation{
    NSLog(@"webViewDidStartLoad");
    [self.activityIndicatorView startAnimating];
}
/**
 *  网页加载完成的时候调用
 */
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    NSLog(@"webViewDidFinishLoad");
    [self.activityIndicatorView stopAnimating];
}
/**
 *  网页加载出错的时候调用
 */
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error{
    if (error != nil) {
        NSLog(@"%@",[error description]);
    }
    [self.activityIndicatorView stopAnimating];
}
/**
 *  在收到响应后，决定是否跳转
 *
 *  @param webView            实现该代理的webview
 *  @param navigationResponse 当前navigation
 *  @param decisionHandler    是否跳转block
 */
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
   
    if ([navigationResponse.response.URL.absoluteString isEqual:@"https://m.qyun88.com/"]||[navigationResponse.response.URL.absoluteString isEqual:@"https://m.qyun88.com/mobile/uc/loginIndex.php"]){
        decisionHandler(WKNavigationResponsePolicyCancel);
        //跳转
//        YTHomeViewController *vc = [[YTHomeViewController alloc]init];
        [self.navigationController popViewControllerAnimated:1];
        return;
    } else{
        decisionHandler(WKNavigationResponsePolicyAllow);
    }

}
/**
 *  在发送请求之前，决定是否跳转
 *
 *  @param webView          实现该代理的webview
 *  @param navigationAction 当前navigation
 *  @param decisionHandler  是否调转block
 */
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    
    if ([navigationAction.request.URL.absoluteString isEqual:@"https://m.qyun88.com/"]){
        decisionHandler(WKNavigationActionPolicyCancel);
          //  YTHomeViewController *vc = [[YTHomeViewController alloc]init];
         [self.navigationController popViewControllerAnimated:1];
        return;
    }else{
        decisionHandler(WKNavigationActionPolicyAllow);
    }
}



@end
